<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="校园文化是以学生为主体，以课外文化活动为主要内容，校园文化建设是以学生为主体，校园为主要空间，涵盖院校领导、教职工在内，以校园精神为主要特征的一种群体文化。校园app，作为学校对外展示校园文化的一个窗口，这一模块的功能也必不可少。科师有约 将这一模块分为 【表白墙 &amp; 图说校园 &amp; 社团活动】 三方面来进行实现。  在这里附一下【河北科技师范学院】校训： 敏学、修身、乐业、创新">
<meta property="og:type" content="article">
<meta property="og:title" content="手把手带你撸一个校园APP（七）：校园文化模块（社团活动&amp;表白墙&amp;图说校园）">
<meta property="og:url" content="http://vchao.top/2018/04/12/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%B8%83%EF%BC%89%EF%BC%9A%E6%A0%A1%E5%9B%AD%E6%96%87%E5%8C%96%E6%A8%A1%E5%9D%97%EF%BC%88%E7%A4%BE%E5%9B%A2%E6%B4%BB%E5%8A%A8&%E8%A1%A8%E7%99%BD%E5%A2%99&%E5%9B%BE%E8%AF%B4%E6%A0%A1%E5%9B%AD%EF%BC%89/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="校园文化是以学生为主体，以课外文化活动为主要内容，校园文化建设是以学生为主体，校园为主要空间，涵盖院校领导、教职工在内，以校园精神为主要特征的一种群体文化。校园app，作为学校对外展示校园文化的一个窗口，这一模块的功能也必不可少。科师有约 将这一模块分为 【表白墙 &amp; 图说校园 &amp; 社团活动】 三方面来进行实现。  在这里附一下【河北科技师范学院】校训： 敏学、修身、乐业、创新">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200213205532771.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200213203006251.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_18,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200214153350437.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200214155401850.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200214103156383.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200214142830872.gif">
<meta property="article:published_time" content="2018-04-12T09:51:40.000Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.807Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="科师有约">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20200213205532771.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70">

<link rel="canonical" href="http://vchao.top/2018/04/12/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%B8%83%EF%BC%89%EF%BC%9A%E6%A0%A1%E5%9B%AD%E6%96%87%E5%8C%96%E6%A8%A1%E5%9D%97%EF%BC%88%E7%A4%BE%E5%9B%A2%E6%B4%BB%E5%8A%A8&%E8%A1%A8%E7%99%BD%E5%A2%99&%E5%9B%BE%E8%AF%B4%E6%A0%A1%E5%9B%AD%EF%BC%89/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>手把手带你撸一个校园APP（七）：校园文化模块（社团活动&表白墙&图说校园） | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2018/04/12/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%B8%83%EF%BC%89%EF%BC%9A%E6%A0%A1%E5%9B%AD%E6%96%87%E5%8C%96%E6%A8%A1%E5%9D%97%EF%BC%88%E7%A4%BE%E5%9B%A2%E6%B4%BB%E5%8A%A8&%E8%A1%A8%E7%99%BD%E5%A2%99&%E5%9B%BE%E8%AF%B4%E6%A0%A1%E5%9B%AD%EF%BC%89/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          手把手带你撸一个校园APP（七）：校园文化模块（社团活动&表白墙&图说校园）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-04-12 17:51:40" itemprop="dateCreated datePublished" datetime="2018-04-12T17:51:40+08:00">2018-04-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-08-25 22:37:20" itemprop="dateModified" datetime="2021-08-25T22:37:20+08:00">2021-08-25</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>校园文化是以学生为主体，以课外文化活动为主要内容，校园文化建设是以学生为主体，校园为主要空间，涵盖院校领导、教职工在内，以校园精神为主要特征的一种群体文化。<br>校园app，作为学校对外展示校园文化的一个窗口，这一模块的功能也必不可少。科师有约 将这一模块分为 【表白墙 &amp; 图说校园 &amp; 社团活动】 三方面来进行实现。</p>
<blockquote>
<p>在这里附一下【河北科技师范学院】校训： 敏学、修身、乐业、创新</p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><h4 id="系列文章"><a href="#系列文章" class="headerlink" title="系列文章"></a>系列文章</h4><p>Github地址： <a target="_blank" rel="noopener" href="https://github.com/zhengweichao/Hevttc">科师有约校园APP</a></p>
<ol>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/80787263">手把手带你撸一个校园APP（一）：项目简介</a></li>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/81456617">手把手带你撸一个校园APP（二）：应用启动和欢迎页面</a></li>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/81837608">手把手带你撸一个校园APP（三）：用户模块（登录注册等）</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/zheng_weichao/article/details/104209803">手把手带你撸一个校园APP（四）：APP框架及功能设计</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/zheng_weichao/article/details/104227390">手把手带你撸一个校园APP（五）：新闻页面中心模块</a></li>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/104245390">手把手带你撸一个校园APP（六）：失物招领&amp;二手交易模块</a></li>
<li><strong>手把手带你撸一个校园APP（七）：校园文化模块（社团活动&amp;表白墙&amp;图说校园）</strong></li>
<li><a href="">手把手带你撸一个校园APP（八）：校园通讯录模块</a></li>
<li>手把手带你撸一个校园APP（九）：课程表模块（模拟登陆爬取教务处课程信息）</li>
<li>手把手带你撸一个校园APP（十）：APP通用模块（更新，意见反馈等）</li>
</ol>
<h2 id="社团组织"><a href="#社团组织" class="headerlink" title="社团组织"></a>社团组织</h2><p>社团活动是校园文化的重要载体，也是学生愉悦身心、拓宽兴趣、开阔视野的主要阵地。<br>丰富多彩的社团活动，能提升学生能力，培养兴趣爱好，激发学生寻找个人特色与才能。每个学校的校园文化也多由此体现。因此设置这个模块，用以展示校园的组织社团等，也为各组织社团提供一种新的线上宣传途径。</p>
<h3 id="实现效果"><a href="#实现效果" class="headerlink" title="实现效果"></a>实现效果</h3><p><img src="https://img-blog.csdnimg.cn/20200213205532771.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70" alt="科师有约社团组织页面"></p>
<h3 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h3><p><strong>数据库</strong><br>| 字段名| 描述 |类型 |是否主键|<br>|—————|————-|————–|——|<br>|objectId     |  唯一标识  |  String     |   是 |<br>|title           | 标题       |  String      |   -  |<br>|description |  描述      |  Number  |  -   |<br>|pic              | 图片       |   String    |  -   |<br>|time           | 发布时间 |  Date       |   -  |</p>
<p><strong>安卓代码</strong><br>查询列表数据，并展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">mAdapter = <span class="keyword">new</span> GeneralAdapter();</span><br><span class="line">teamBeen = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">rvTeam.setLayoutManager(<span class="keyword">new</span> LinearLayoutManager(TeamActivity.<span class="keyword">this</span>));</span><br><span class="line">BmobQuery&lt;TeamBean&gt; query = <span class="keyword">new</span> BmobQuery&lt;TeamBean&gt;();</span><br><span class="line">query.order(<span class="string">&quot;-time&quot;</span>);</span><br><span class="line">query.setLimit(<span class="number">15</span>);</span><br><span class="line">query.findObjects(<span class="keyword">new</span> FindListener&lt;TeamBean&gt;() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">done</span><span class="params">(List&lt;TeamBean&gt; object, BmobException e)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (e == <span class="keyword">null</span>) &#123;</span><br><span class="line">            LogUtils.e(<span class="string">&quot;查询成功：共&quot;</span> + object.size() + <span class="string">&quot;条数据。&quot;</span>);</span><br><span class="line">            teamBeen.addAll(object);</span><br><span class="line">            mAdapter.setData(teamBeen);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            LogUtils.e(<span class="string">&quot;失败：&quot;</span> + e.getMessage() + <span class="string">&quot;,&quot;</span> + e.getErrorCode());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">rvTeam.setAdapter(mAdapter);</span><br></pre></td></tr></table></figure>

<h2 id="表白墙"><a href="#表白墙" class="headerlink" title="表白墙"></a>表白墙</h2><p>近年来，表白墙作为大学校园一种新的社交平台迅速崛起，不仅流行范围广、传播速度快，而且显得异常活跃。这种别具一格的表达方式，由于具有鲜明的时代特点和浓厚的感情色彩，深受大学生的青睐。</p>
<h3 id="效果"><a href="#效果" class="headerlink" title="效果"></a>效果</h3><p>表白墙及发布页面如下：<br><img src="https://img-blog.csdnimg.cn/20200213203006251.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_18,color_FFFFFF,t_70" alt="科师有约表白墙"></p>
<blockquote>
<p>本人实在没有美术与设计功底，审美也常年掉线。还望勿笑~~</p>
</blockquote>
<h3 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h3><p>从代码层面来分析的话，其实就是数据的发布与查询操作。<br>这部分代码重复性较高，此处就不再赘述了。着重讲下表白信的背景图片【9-patch 图片】。</p>
<h3 id="9-patch-图片"><a href="#9-patch-图片" class="headerlink" title="9-patch 图片"></a>9-patch 图片</h3><p>9-patch 图片常叫做 .9图片。因为文件的后缀名是 .9.png 。<br>是一个指定一些区域可拉伸，另外一些区域不拉伸的位图，同时还可以指定显示内容的区域。在Android开发中，经常使用到.9图片，比如聊天页的消息背景等，以避免因内容导致位图变形。<br>给一个对比图来感受一下，使用和不使用的区别（左：不使用，右：使用）：<br><img src="https://img-blog.csdnimg.cn/20200214153350437.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70" alt="9-patch图片"><br>可以看到，使用普通图片的话，变形会很严重，而使用9patch则会好很多。<br><strong>如何制作9patch图片</strong><br>制作.9图片有很多种方法，最方便快捷的莫过于在Android Studio中进行制作。具体步骤如下：</p>
<p>step1. 准备png图片，需要至少预留1像素的透明区域，然后放到项目drawable或mipmap文件中；<br>step2. 选中该文件，右键选择 “Create 9-Patch file …”，之后drawable 文件夹下回出现同名的 .9.png 图片；<br>step3.  绘制可拉伸区域（在顶部以及左侧划线表示，绘制页面如下图所示，右侧可预览拉伸后的效果）；<br>step4.  和其它图片一样正常使用即可。<br><img src="https://img-blog.csdnimg.cn/20200214155401850.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70" alt="制作9-Patch图片"></p>
<h2 id="图说校园"><a href="#图说校园" class="headerlink" title="图说校园"></a>图说校园</h2><h3 id="效果-1"><a href="#效果-1" class="headerlink" title="效果"></a>效果</h3><p><img src="https://img-blog.csdnimg.cn/20200214103156383.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h3 id="分析-1"><a href="#分析-1" class="headerlink" title="分析"></a>分析</h3><p>这一部分逻辑比较简单，同样是请求数据列表然后展示。<br>不同的是，列表采用了瀑布流的形式展示，其实 RecycleView 可以轻易实现这种效果。<br>数据请求部分代码重复性较高，在此就不再赘述了。这里着重讲跳转页面时的转场动画【共享元素】的实现。<br><strong>共享元素(Shared Element)</strong><br>共享元素属于 Material Design， 为场景切换提供了非常优雅的视觉效果，在进入和退出页面，元素共享页面的时候可以添加想要的效果，能给人元素在运动的感觉。Gif 图如下：<br><img src="https://img-blog.csdnimg.cn/20200214142830872.gif" alt="图说校园共享元素"></p>
<h3 id="实现-1"><a href="#实现-1" class="headerlink" title="实现"></a>实现</h3><h4 id="数据库"><a href="#数据库" class="headerlink" title="数据库"></a>数据库</h4><p>数据库字段设计如下：</p>
<table>
<thead>
<tr>
<th>字段名</th>
<th>描述</th>
<th>类型</th>
<th>是否主键</th>
</tr>
</thead>
<tbody><tr>
<td>objectId</td>
<td>id</td>
<td>String</td>
<td>是</td>
</tr>
<tr>
<td>name</td>
<td>标题</td>
<td>String</td>
<td>-</td>
</tr>
<tr>
<td>description</td>
<td>描述</td>
<td>Number</td>
<td>-</td>
</tr>
<tr>
<td>pic</td>
<td>图片</td>
<td>String</td>
<td>-</td>
</tr>
<tr>
<td>createdAt</td>
<td>发布时间</td>
<td>Date</td>
<td>-</td>
</tr>
</tbody></table>
<p>查询列表等数据请求操作部分代码重复性较高，可参考之前文件代码。在此就不再赘述了。</p>
<h4 id="共享元素"><a href="#共享元素" class="headerlink" title="共享元素"></a>共享元素</h4><p>Step1:</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">name</span>=<span class="string">&quot;AppTheme&quot;</span> <span class="attr">parent</span>=<span class="string">&quot;Theme.AppCompat.Light&quot;</span>&gt;</span><span class="xml"></span></span><br><span class="line"><span class="xml">       <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowContentTransitions&quot;</span>&gt;</span>true<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>Step2:<br>为共享元素设置 transtionName，两个界面都需要要添加，用以标识哪两个元素共享。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--已省略其它属性--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ImageView</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">&quot;274dp&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">&quot;307dp&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:transitionName</span>=<span class="string">&quot;shareView&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>
<p>当然也可以在代码中设置：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">imageView.setTransitionName(<span class="string">&quot;shareView&quot;</span>);</span><br></pre></td></tr></table></figure>
<p>Step3:<br>设置好以上的参数后，就可以通过跳转来添加相应的动画了。需要用到 ActivityOptionsCompat 的 <strong>makeSceneTransitionAnimation</strong> 函数。代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Intent i = <span class="keyword">new</span> Intent(mContext, PicDetailActivity.class);</span><br><span class="line">ActivityOptionsCompat optionsCompat = </span><br><span class="line">ActivityOptionsCompat.makeSceneTransitionAnimation(mContext, holder.image, <span class="string">&quot;shareView&quot;</span>);</span><br><span class="line">startActivity(i,optionsCompat.toBundle());</span><br></pre></td></tr></table></figure>
<p>设置多个元素共享的代码如下:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Intent i = <span class="keyword">new</span> Intent(mContext, PicDetailActivity.class);</span><br><span class="line">Pair&lt;View, String&gt; pair = <span class="keyword">new</span> Pair&lt;View, String&gt;(holder.image,<span class="string">&quot;shareView&quot;</span>);</span><br><span class="line">Pair&lt;View,String&gt; pairText = <span class="keyword">new</span> Pair&lt;View, String&gt;(holder.text,<span class="string">&quot;text&quot;</span>);</span><br><span class="line">ActivityOptionsCompat optionsCompat = </span><br><span class="line">ActivityOptionsCompat.makeSceneTransitionAnimation(mContext, pair,pairText);</span><br><span class="line">startActivity(i,optionsCompat.toBundle());</span><br></pre></td></tr></table></figure>



<hr>
<p>至此，本文内容全部完毕，项目中内容均为简易实现，深究的话，可以优化的细节极多，然而作者可能并没有更大的精力去全部完善。此项目也权当抛砖引玉之用，望以后可以有学弟学妹做出真正的产品。<br>另外，项目中大量依赖或采用了第三方项目的代码。在此向各位有分享精神的大佬们致敬！</p>
<p>如果本文对你有所帮助，还望可以随手赏一个点赞哈 ~ ~</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/%E7%A7%91%E5%B8%88%E6%9C%89%E7%BA%A6/" rel="tag"># 科师有约</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2018/04/12/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%B8%89%EF%BC%89%EF%BC%9A%E7%94%A8%E6%88%B7%E6%A8%A1%E5%9D%97%EF%BC%88%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E7%AD%89%EF%BC%89/" rel="prev" title="手把手带你撸一个校园APP（三）：用户模块（登录注册等）">
      <i class="fa fa-chevron-left"></i> 手把手带你撸一个校园APP（三）：用户模块（登录注册等）
    </a></div>
      <div class="post-nav-item">
    <a href="/2018/04/12/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%B8%80%EF%BC%89%EF%BC%9A%E9%A1%B9%E7%9B%AE%E7%AE%80%E4%BB%8B/" rel="next" title="手把手带你撸一个校园APP（一）：项目简介">
      手把手带你撸一个校园APP（一）：项目简介 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%B3%BB%E5%88%97%E6%96%87%E7%AB%A0"><span class="nav-number">1.0.1.</span> <span class="nav-text">系列文章</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%A4%BE%E5%9B%A2%E7%BB%84%E7%BB%87"><span class="nav-number">2.</span> <span class="nav-text">社团组织</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E6%95%88%E6%9E%9C"><span class="nav-number">2.1.</span> <span class="nav-text">实现效果</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0"><span class="nav-number">2.2.</span> <span class="nav-text">实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A1%A8%E7%99%BD%E5%A2%99"><span class="nav-number">3.</span> <span class="nav-text">表白墙</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%88%E6%9E%9C"><span class="nav-number">3.1.</span> <span class="nav-text">效果</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E6%9E%90"><span class="nav-number">3.2.</span> <span class="nav-text">分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-patch-%E5%9B%BE%E7%89%87"><span class="nav-number">3.3.</span> <span class="nav-text">9-patch 图片</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E8%AF%B4%E6%A0%A1%E5%9B%AD"><span class="nav-number">4.</span> <span class="nav-text">图说校园</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%88%E6%9E%9C-1"><span class="nav-number">4.1.</span> <span class="nav-text">效果</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E6%9E%90-1"><span class="nav-number">4.2.</span> <span class="nav-text">分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0-1"><span class="nav-number">4.3.</span> <span class="nav-text">实现</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">4.3.1.</span> <span class="nav-text">数据库</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%85%B1%E4%BA%AB%E5%85%83%E7%B4%A0"><span class="nav-number">4.3.2.</span> <span class="nav-text">共享元素</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
